package com.sanjiahantang.ythshopping.presenter;

import android.content.Context;

import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.http.BaseDelegate;
import com.sanjiahantang.ythshopping.http.ExceptionHelper;
import com.sanjiahantang.ythshopping.http.OkHttpClientManager;
import com.sanjiahantang.ythshopping.model.BaseModel;
import com.sanjiahantang.ythshopping.utils.CheckUtil;
import com.sanjiahantang.ythshopping.utils.Utils;
import com.sanjiahantang.ythshopping.views.SendCodeView;
import com.squareup.okhttp.Request;

import java.util.Map;

/**
 * Created by sks on 2015/9/29.
 * 发送验证码
 */
public class SendCodePresenter extends BasePresenter {
    private SendCodeView sendCodeView;
    public SendCodePresenter(SendCodeView sendCodeView){
        this.sendCodeView = sendCodeView;
    }
    public void setSendCodeView(final Context context,String name,String email){
        initLoadDialog(context);
        mLoadingDialog.show();
        String url = "user/send";
        Map<String,String> params = getDefaultMD5Params("user","send");
        params.put("username",name);
        if(CheckUtil.isMobileNO(email)){
            url = "user/send_mobile";
            params.put("mobile",email);
        }else {
            params.put("email",email);
        }

        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + url,
                params, new BaseDelegate.ResultCallback<BaseModel>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        dismiss();
                        Utils.showToast(context, ExceptionHelper.getMessage(e, context));
                    }

                    @Override
                    public void onResponse(BaseModel response, Object tag) {
                        dismiss();
                        sendCodeView.getCode(response);
                    }
                },true);
    }
}
